<?php

namespace app\admin\controller;

use app\common\Helper;
use app\common\validate\BusinessareaValidate;

use think\Exception;
use think\Request;

class Businessarea extends Base
{

    public function getlist(){
        $params = $this->request->param();
        $where = [
            'status' => 1
        ];
        if(isset($params['begin_time']) && $params['begin_time']){
            $where['createDate'] = ['egt', $params['begin_time']];
        }
        if(isset($params['end_time']) && $params['end_time']){
            $where['createDate'] = ['lt', $params['end_time'] . ' 23:59:59'];
        }
        if(isset($params['begin_time']) && $params['begin_time'] && isset($params['end_time']) && $params['end_time']){
            $where['createDate'] = [['egt', $params['begin_time']], ['lt', $params['end_time'] . ' 23:59:59'], 'and'];
        }
        if(isset($params['globalName']) && $params['globalName']){
            $where['globalName'] = ['like', "%{$params['globalName']}%"];
        }

        $model = model('Businessarea');
        $list = $model->where($where)->order('createDate desc')->paginate(10);
        $total = $model->where($where)->count();

        return $this->fetch('',[
            'datas' => $list,
            'total' => $total,
            'formget' => $_GET
        ]);
    }


    public function add(){
        return $this->fetch();
    }

    public function edit(Request $request){
        $param = $request->param();
        if(empty($param['id'])){
            return show(config('status.error_code'),'缺少参数','缺少参数');
        }
        $Businessarea = \app\admin\model\Businessarea::get($param['id']);

        return $this->fetch('',[
            'data' => $Businessarea
        ]);
    }


    public function addoreditpost(Request $request){
        if($request->isPost()){
            try{
                (new BusinessareaValidate())->goCheck();
            }catch(Exception $ex){
                return show(config('status.error_code'),'参数不合法','参数不合法');
            }
            $param = $request->param();
            $latandlng = explode(',',$param['addressGeo']);
            if(empty($param['buissId'])){
                //添加
                $model = new \app\admin\model\Businessarea();
                $id = Helper::getUUID();
                $model->id = $id;
            }else{
                //修改
                $model = \app\admin\model\Businessarea::get($param['buissId']);
                if(!$model){
                    return show(config('status.error_code'),'数据异常','数据异常');
                }
            }
            $model->globalName = $param['globalName'];
            $model->addressName = $param['input_address'];
            $model->province = $param['province'];
            $model->city = $param['city'];
            $model->district = $param['area'];
            $model->lat = (float)$latandlng[0];
            $model->lng = (float)$latandlng[1];
            if($model->save()){
                return show(config('status.success_code'),'保存成功','保存成功');
            }else{
                return show(config('status.error_code'),'保存失败','保存失败');
            }

        }else{
            return show(config('status.error_code'),'请求不合法','请求不合法');

        }
    }

}